#include "pch.h"
#include "CSkinEdit.h"

IMPLEMENT_DYNAMIC(CSkinEdit, CEdit)

CSkinEdit::CSkinEdit()
{
	m_bTracking = m_bHover = m_bFocus = FALSE;
	//m_clientBrush = ::CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
	m_normalBrush = ::CreateSolidBrush(GetSysColor(COLOR_WINDOWFRAME));
	m_hangOnBrush = ::CreateSolidBrush(RGB(0, 255, 0));
	m_focusBrush = ::CreateSolidBrush(RGB(255, 0, 0));
}

CSkinEdit::~CSkinEdit()
{
	DeleteObject(m_normalBrush);
	DeleteObject(m_hangOnBrush);
	DeleteObject(m_focusBrush);

}

void CSkinEdit::DrawBorder()
{
	HDC hDC = ::GetWindowDC(m_hWnd);
	CRect rc;
	::GetWindowRect(m_hWnd, &rc);
	rc = CRect(0, 0, rc.right - rc.left, rc.bottom - rc.top);	// Get the control outline
	if (m_bHover) {
		::FrameRect(hDC, &rc, m_hangOnBrush);
		rc.InflateRect(-1, -1);		// You can draw the thick outline through this way
		::FrameRect(hDC, &rc, m_hangOnBrush);
	}
	if (m_bFocus) {
		::FrameRect(hDC, &rc, m_focusBrush);
		rc.InflateRect(-1, -1);
		::FrameRect(hDC, &rc, m_focusBrush);
	}
	else {
		::FrameRect(hDC, &rc, m_normalBrush);
	}
	::ReleaseDC(m_hWnd, hDC);
}

BEGIN_MESSAGE_MAP(CSkinEdit, CEdit)
	ON_WM_MOUSELEAVE()
	ON_WM_MOUSEHOVER()
	ON_WM_NCPAINT()
	ON_WM_MOUSEMOVE()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
END_MESSAGE_MAP()

/*
* Describe	: If you leave the mouse from this control, it will run this function
* Notes		: It must be used with OnMouseMove() -> tme.dwFlags.
*			  If you don't just set "tme.dwFlags = TME_HOVER;", then this funstion will be never called.
* 
* Author	: CL Wu
* Date		: 2021/12/17
*/
void CSkinEdit::OnMouseLeave()
{
	m_bTracking = FALSE;
	m_bHover = FALSE;
	DrawBorder();
}

/*
* Describe	: If you hang the mouse on this control, it will run this function
* Notes		: It must be used with OnMouseMove() -> tme.dwFlags.
*			  If you don't just set "tme.dwFlags = TME_LEAVE;", then this funstion will be never called.
* 
* Author	: CL Wu
* Date		: 2021/12/17
*/
void CSkinEdit::OnMouseHover(UINT nFlags, CPoint point)
{
	m_bHover = TRUE;
	DrawBorder();
}

/*
* Describe	: When the rect which out of window client(like window outline) needs to be redraw, it will run this function
*
* Author	: CL Wu
* Date		: 2021/12/17
*/
void CSkinEdit::OnNcPaint()
{
	CEdit::OnNcPaint();
	DrawBorder();
}

/*
* Describe	: If you move the mouse on the rect of this control, it will run this function
* Notes		: tme.dwFlags means the mouse messages you want to control
*
* Author	: CL Wu
* Date		: 2021/12/17
*/
void CSkinEdit::OnMouseMove(UINT nFlags, CPoint point)
{
	if (!m_bTracking)
	{
		TRACKMOUSEEVENT tme;
		tme.cbSize = sizeof(tme);
		tme.hwndTrack = m_hWnd;
		tme.dwFlags = TME_LEAVE | TME_HOVER;		// Set the tracking request
		tme.dwHoverTime = 50;
		m_bTracking = (bool)_TrackMouseEvent(&tme);
	}


	CEdit::OnMouseMove(nFlags, point);
}

/*
* Describe	: If you click this control, it will run this function
*
* Author	: CL Wu
* Date		: 2021/12/17
*/
void CSkinEdit::OnSetFocus(CWnd* pOldWnd)
{
	CEdit::OnSetFocus(pOldWnd);

	m_bFocus = TRUE;
	DrawBorder();
}

/*
* Describe	: If you click out of this control, it will run this function
*
* Author	: CL Wu
* Date		: 2021/12/17
*/
void CSkinEdit::OnKillFocus(CWnd* pNewWnd)
{
	CEdit::OnKillFocus(pNewWnd);

	m_bFocus = FALSE;
	DrawBorder();
}
